﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastData;

namespace FastCtr
{
    public partial class NewDataGridView : UserControl
    {
        private int _pageSize;
        private int currentPageNum;
        private DataPageSource _DataPageSource;
        private bool _scrollToLast;

        public NewDataGridView()
        {
            InitializeComponent();
        }

        public bool ScrollToLast
        {
            set
            {
                _scrollToLast = value;
            }
            get
            {
                return _scrollToLast;
            }
        }


        public int CurrentPageNum
        {
            //get
            //{
            //    return currentPageNum;
            //}
            set
            {
                if(_DataPageSource==null)
                {
                    return;
                }
                if (value > _DataPageSource.TotalPageCount)
                {
                    throw new Exception($"页数 {value} 不存在");
                }
                else
                {
                    currentPageNum = value;
                    lbPage.Text = string.Format("第{0}页/共{1}页", currentPageNum, _DataPageSource.TotalPageCount);
                    dgvProduct.DataSource = _DataPageSource[currentPageNum];
                    SelfStyle();
                }
            }
        }
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }

        public DataPageSource DataPageSource
        {
            set
            {
                _DataPageSource = value;
                if (_scrollToLast)
                {
                    CurrentPageNum = _DataPageSource.TotalPageCount;
                    dgvProduct.FirstDisplayedScrollingRowIndex = dgvProduct.Rows.Count - 1;
                }
                else
                {
                    CurrentPageNum = 1;
                }
            }
        }

        private void SelfStyle()
        {
            for (int i = 0; i < dgvProduct.ColumnCount; i++)
            {
                dgvProduct.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
            }

            foreach (DataGridViewColumn dc in dgvProduct.Columns)
            {
                dc.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                dc.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            }
        }

        /// <summary>
        /// 第一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            CurrentPageNum = 1;
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpPage_Click(object sender, EventArgs e)
        {
            if (currentPageNum == 1)
            {
                MessageBox.Show("已是第一页！");
                return;
            }
            currentPageNum--;
            CurrentPageNum = currentPageNum;
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if (currentPageNum == _DataPageSource.TotalPageCount)
            {
                MessageBox.Show("已是最后一页！");
                return;
            }
            currentPageNum++;
            CurrentPageNum = currentPageNum;
        }

        /// <summary>
        /// 最后一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEndPage_Click(object sender, EventArgs e)
        {
            CurrentPageNum = _DataPageSource.TotalPageCount;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int linescount = 0;
            int.TryParse(textBox1.Text, out linescount);

        }
    }
}
